package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.service.NursingRecordPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 护理记录的定位
 */

@RequestMapping("NursingRecordPosition")
@RestController
public class NursingRecordPositionController extends BaseController{

    @Autowired
    @Qualifier("NursingRecordPositionServiceImplOne")
    private NursingRecordPositionService nursingRecordPositionService;


    @RequestMapping(value = "List", method = RequestMethod.POST)
    public MyResult List(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String nursingRecordCode = jsonObject.getString("nursingRecordCode");

            PreCheck preCheck =checkSignFrontInterface(timeStr,sign,"/NursingRecordPosition/List");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            List<NursingRecordPosition> list = nursingRecordPositionService.listByNursingRecordCode(nursingRecordCode);

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("list",list);

            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/NursingRecordPosition/List", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //打卡
    @RequestMapping(value = "UpdateStatus", method = RequestMethod.POST)
    public MyResult UpdateStatus(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String nursingRecordPositionCode = jsonObject.getString("nursingRecordPositionCode");
            String lon = jsonObject.getString("lon");
            String lat = jsonObject.getString("lat");
            String address = jsonObject.getString("address");

            PreCheck preCheck =checkSignFrontInterface(timeStr,sign,"/NursingRecordPosition/UpdateStatus");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            NursingRecordPosition nursingRecordPosition = nursingRecordPositionService.getByNursingRecordPositionCode(nursingRecordPositionCode);

            Date ct = nursingRecordPosition.getCreateTime();
            Date now = new Date();
            if(now.getTime() - ct.getTime() > 5*60*1000){
                return failed("已超时");
            }

            nursingRecordPosition.setLat(lat);
            nursingRecordPosition.setLon(lon);
            nursingRecordPosition.setAddress(address);
            nursingRecordPosition.setStatus(1);
            nursingRecordPositionService.update(nursingRecordPosition);

            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/NursingRecordPosition/UpdateStatus", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


}
